<!DOCTYPE html>
<html lang="zh-Hans">
  <head>
    
<meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>


<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />

<meta name="theme-color" content="#f8f5ec" />
<meta name="msapplication-navbutton-color" content="#f8f5ec">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="#f8f5ec">



  <meta name="description" content="逛v2的时候看到一个帖子:[点击这里](http://v2ex.com/t/133080#reply45) 各个码农都在秀自家的语言,身为php程序员,也尝试着写了一个,没想到这么一个简单的问题也踩了不少的坑."/>




  <meta name="keywords" content="技术, PHP, 杂鱼君的博客" />










  <link rel="alternate" href="/atom.xml" title="杂鱼君的博客">




  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=2.6.0" />



<link rel="canonical" href="http://notes.wen.moe/post/how-to-print-all-five-letters-string-by-php.html"/>


<link rel="stylesheet" type="text/css" href="/css/style.css?v=2.6.0" />



  <link rel="stylesheet" type="text/css" href="/lib/fancybox/jquery.fancybox.css" />




  



  <script id="baidu_push">
(function(){
    var bp = document.createElement('script');
    var curProtocol = window.location.protocol.split(':')[0];
    if (curProtocol === 'https') {
        bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
    }
    else {
        bp.src = 'http://push.zhanzhang.baidu.com/push.js';
    }
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(bp, s);
})();
</script>









    <title> 如何用PHP输出26个英文字母组成的所有5位字符串? - 杂鱼君的博客 </title>
  </head>

  <body><div id="mobile-navbar" class="mobile-navbar">
  <div class="mobile-header-logo">
    <a href="/." class="logo">杂鱼君的博客</a>
  </div>
  <div class="mobile-navbar-icon">
    <span></span>
    <span></span>
    <span></span>
  </div>
</div>

<nav id="mobile-menu" class="mobile-menu slideout-menu">
  <ul class="mobile-menu-list">
    
      <a href="/">
        <li class="mobile-menu-item">
          
          
            Home
          
        </li>
      </a>
    
      <a href="/archives/">
        <li class="mobile-menu-item">
          
          
            Archives
          
        </li>
      </a>
    
      <a href="/tags">
        <li class="mobile-menu-item">
          
          
            Tags
          
        </li>
      </a>
    
      <a href="/about">
        <li class="mobile-menu-item">
          
          
            About
          
        </li>
      </a>
    
  </ul>
</nav>

    <div class="container" id="mobile-panel">
      <header id="header" class="header"><div class="logo-wrapper">
  <a href="/." class="logo">杂鱼君的博客</a>
</div>

<nav class="site-navbar">
  
    <ul id="menu" class="menu">
      
        <li class="menu-item">
          <a class="menu-item-link" href="/">
            
            
              Home
            
          </a>
        </li>
      
        <li class="menu-item">
          <a class="menu-item-link" href="/archives/">
            
            
              Archives
            
          </a>
        </li>
      
        <li class="menu-item">
          <a class="menu-item-link" href="/tags">
            
            
              Tags
            
          </a>
        </li>
      
        <li class="menu-item">
          <a class="menu-item-link" href="/about">
            
            
              About
            
          </a>
        </li>
      
    </ul>
  
</nav>

      </header>

      <main id="main" class="main">
        <div class="content-wrapper">
          <div id="content" class="content">
            
  
  <article class="post">
    <header class="post-header">
      <h1 class="post-title">
        
          如何用PHP输出26个英文字母组成的所有5位字符串?
        
      </h1>

      <div class="post-meta">
        <span class="post-time">
          2014-01-17
        </span>
        
          <div class="post-category">
            
              <a href="/categories/技术/">技术</a>
            
          </div>
        
        
      </div>
    </header>

    
    
  <div class="post-toc" id="post-toc">
    <h2 class="post-toc-title">Contents</h2>
    <div class="post-toc-content">
      <ol class="toc"><li class="toc-item toc-level-4"><a class="toc-link" href="#比较大小"><span class="toc-text">比较大小</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#字符串的自增-只限英文字母"><span class="toc-text">字符串的自增(只限英文字母)</span></a></li></ol>
    </div>
  </div>


    <div class="post-content">
      
        <p>逛v2的时候看到一个帖子:<a href="http://v2ex.com/t/133080#reply45" target="_blank" rel="external">点击这里</a> 各个码农都在秀自家的语言,身为php程序员,也尝试着写了一个,没想到这么一个简单的问题也踩了不少的坑.</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div></pre></td><td class="code"><pre><div class="line">$arr=<span class="keyword">array</span>(<span class="string">"a"</span>,<span class="string">"b"</span>,<span class="string">'c'</span>,<span class="string">'d'</span>,<span class="string">'e'</span>,<span class="string">'f'</span>,<span class="string">'g'</span>,<span class="string">'h'</span>,<span class="string">'i'</span>,<span class="string">'j'</span>,<span class="string">'k'</span>,<span class="string">'l'</span>,<span class="string">'m'</span>,<span class="string">'n'</span>,<span class="string">'o'</span>,<span class="string">'p'</span>,<span class="string">'q'</span>,<span class="string">'r'</span>,<span class="string">'s'</span>,<span class="string">'t'</span>,<span class="string">'u'</span>,<span class="string">'v'</span>,<span class="string">'w'</span>,<span class="string">'x'</span>,<span class="string">'y'</span>,<span class="string">'z'</span>);</div><div class="line">	$num=<span class="keyword">array</span>(<span class="number">0</span>,<span class="number">0</span>,<span class="number">0</span>,<span class="number">0</span>,<span class="number">0</span>);</div><div class="line">	$out=<span class="string">''</span>;</div><div class="line">	<span class="keyword">while</span>($num[<span class="number">4</span>]&lt;=<span class="number">25</span>)&#123;</div><div class="line">		$out.=$arr[$num[<span class="number">0</span>]].$arr[$num[<span class="number">1</span>]].$arr[$num[<span class="number">2</span>]].$arr[$num[<span class="number">3</span>]].$arr[$num[<span class="number">4</span>]].<span class="string">','</span>;</div><div class="line">		$num[<span class="number">0</span>]++;</div><div class="line">		<span class="keyword">if</span>($num[<span class="number">0</span>]&gt;<span class="number">25</span>)&#123;</div><div class="line">			$num[<span class="number">0</span>]=<span class="number">0</span>;</div><div class="line">			$num[<span class="number">1</span>]++;</div><div class="line">		&#125;</div><div class="line">		<span class="keyword">if</span>($num[<span class="number">1</span>]&gt;<span class="number">25</span>)&#123;</div><div class="line">			$num[<span class="number">1</span>]=<span class="number">0</span>;</div><div class="line">			$num[<span class="number">2</span>]++;</div><div class="line">		&#125;</div><div class="line">		<span class="keyword">if</span>($num[<span class="number">2</span>]&gt;<span class="number">25</span>)&#123;</div><div class="line">			$num[<span class="number">2</span>]=<span class="number">0</span>;</div><div class="line">			$num[<span class="number">3</span>]++;</div><div class="line">		&#125;</div><div class="line">		<span class="keyword">if</span>($num[<span class="number">3</span>]&gt;<span class="number">25</span>)&#123;</div><div class="line">			$num[<span class="number">3</span>]=<span class="number">0</span>;</div><div class="line">			$num[<span class="number">4</span>]++;</div><div class="line">		&#125;</div><div class="line">	&#125;</div><div class="line">	file_put_contents(<span class="string">"d:/num.txt"</span>, $out);</div></pre></td></tr></table></figure>
<p>虽然结果是正确的,但是这滑稽的判断连我自己都忍不住放声大笑……幸好另一位php前辈写了如下这个:</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">for</span>($i=<span class="string">'aaaaa'</span>;$i&lt;<span class="string">'zzzzz'</span>;$i++)&#123;</div><div class="line">    <span class="keyword">echo</span> $i.<span class="string">','</span>;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>好吧,PHP中字符串之间也可以比大小这是知道的,但是加减的规则是怎样的呢?</p>
<h4 id="比较大小"><a href="#比较大小" class="headerlink" title="比较大小"></a>比较大小</h4><p>这个很简单,php手册上写的很清楚:</p>
<ul>
<li>从第一个字符开始,比较ascii码</li>
<li>如果相同,再比较第二个字符</li>
<li>与字符串的长度无关<br>例如 “aaaaa”和”z” 比较,由于第一个”z”大于”a”,所以”z”&gt;”aaaaa”</li>
</ul>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">    $a=<span class="string">'aaaaa'</span>;$b=<span class="string">'z'</span>;</div><div class="line"><span class="keyword">echo</span> $b&gt;$a?<span class="string">"yes"</span>:<span class="string">"no"</span>;<span class="comment">//输出'yes'</span></div></pre></td></tr></table></figure>
<h4 id="字符串的自增-只限英文字母"><a href="#字符串的自增-只限英文字母" class="headerlink" title="字符串的自增(只限英文字母)"></a>字符串的自增(只限英文字母)</h4><p>stackoveflow上有关于这个的讨论:<a href="http://stackoverflow.com/questions/4098345/why-doesnt-this-code-simply-print-letters-a-to-z" target="_blank" rel="external">点击这里</a><br>帖子里面讨论的很详细,php关于字母的加减的规则没有沿袭C语言,而是模仿perl,”z”++ 并不是等于”[“(C语言是这样),而是等于”aa”.</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">$str1=<span class="string">"a"</span>;</div><div class="line"><span class="keyword">echo</span> ++$str1;<span class="comment">//"b"</span></div><div class="line">$str2=<span class="string">"z"</span>;</div><div class="line"><span class="keyword">echo</span> ++$str2;<span class="comment">//"aa"</span></div><div class="line">$str2=<span class="string">"z"</span>;</div><div class="line"><span class="keyword">echo</span> --$str2;<span class="comment">//"z"</span></div></pre></td></tr></table></figure>
<p>注意只能自增,自减无效果.并且:</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">$str=<span class="string">"a"</span>;</div><div class="line">$a=++$str;</div><div class="line">$b=$str+<span class="number">1</span>;</div><div class="line"><span class="keyword">echo</span> $a;<span class="comment">//"b"</span></div><div class="line"><span class="keyword">echo</span> $b;<span class="comment">// 1</span></div></pre></td></tr></table></figure>
<p>字符串与数字加减,依然走php关于这方面的规定.</p>
<p>所以,如何用php方便的输出a-z?</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">for</span>($i=<span class="string">"a"</span>;$i&lt;<span class="string">"z"</span>;$i++)&#123;</div><div class="line">    <span class="keyword">echo</span> $i.<span class="string">"&lt;br&gt;"</span>;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>以上</p>

      
    </div>

    
      
      

  <div class="post-copyright">
    <p class="copyright-item">
      <span>Author: </span>
      <a href="http://notes.wen.moe">zakokun</a>
    </p>
    <p class="copyright-item">
      <span>Link: </span>
      <a href="http://notes.wen.moe/post/how-to-print-all-five-letters-string-by-php.html">http://notes.wen.moe/post/how-to-print-all-five-letters-string-by-php.html</a>
    </p>
    <p class="copyright-item">
      <span>License: </span>
      
      <a rel="license" href="http://creativecommons.org/licenses/by-nc/4.0/" target="_blank">知识共享署名-非商业性使用 4.0 国际许可协议</a>
    </p>
  </div>



      
      
    

    
      <footer class="post-footer">
        
          <div class="post-tags">
            
              <a href="/tags/技术/">技术</a>
            
              <a href="/tags/PHP/">PHP</a>
            
          </div>
        
        
        
  <nav class="post-nav">
    
      <a class="prev" href="/post/sublime-plugin-introduce.html">
        <i class="iconfont icon-left"></i>
        <span class="prev-text nav-default">sublime的一些个人使用心得</span>
        <span class="prev-text nav-mobile">Prev</span>
      </a>
    
    
  </nav>

      </footer>
    

  </article>


          </div>
          
  <div class="comments" id="comments">
    
      <div id="disqus_thread">
        <noscript>
          Please enable JavaScript to view the
          <a href="//disqus.com/?ref_noscript">comments powered by Disqus.</a>
        </noscript>
      </div>
    
  </div>


        </div>
      </main>

      <footer id="footer" class="footer">

  <div class="social-links">
    
      
        
          <a href="mailto:me@wen.moe" class="iconfont icon-email" title="email"></a>
        
      
    
      
    
      
    
      
    
      
    
      
    
      
    
      
    
      
    
      
    
      
    
      
    
      
        
          <a href="https://www.instagram.com/zakokun" class="iconfont icon-instagram" title="instagram"></a>
        
      
    
    
    
  </div>


<div class="copyright">
  <span class="power-by">
    Powered by <a class="hexo-link" href="https://hexo.io/">Hexo</a>
  </span>
  <span class="division">|</span>
  <span class="theme-info">
    Theme -
    <a class="theme-link" href="https://github.com/ahonn/hexo-theme-even">Even</a>
  </span>
  <span class="division">|</span>
  <span class="theme-info">
    Hosted by <a class="theme-link" href="https://pages.coding.me" style="font-weight: bold">Coding Pages</a>
  </span>

  <span class="copyright-year">
    
    &copy;
    
      2014 -
    
    2017

    <span class="heart">
      <i class="iconfont icon-heart"></i>
    </span>
    <span class="author">zakokun</span>
  </span>
</div>

      </footer>

      <div class="back-to-top" id="back-to-top">
        <i class="iconfont icon-up"></i>
      </div>
    </div>

    
  
  <script type="text/javascript">
    var disqus_config = function () {
        this.page.url = 'http://notes.wen.moe/post/how-to-print-all-five-letters-string-by-php.html';
        this.page.identifier = 'post/how-to-print-all-five-letters-string-by-php.html';
        this.page.title = '如何用PHP输出26个英文字母组成的所有5位字符串?';
    };
    (function() {
    var d = document, s = d.createElement('script');

    s.src = '//evanszhang.disqus.com/embed.js';

    s.setAttribute('data-timestamp', +new Date());
    (d.head || d.body).appendChild(s);
    })();  
  </script>




    
  





  
    <script type="text/javascript" src="/lib/jquery/jquery-3.1.1.min.js"></script>
  

  
    <script type="text/javascript" src="/lib/slideout/slideout.js"></script>
  

  
    <script type="text/javascript" src="/lib/fancybox/jquery.fancybox.pack.js"></script>
  


    <script type="text/javascript" src="/js/src/even.js?v=2.6.0"></script>
<script type="text/javascript" src="/js/src/bootstrap.js?v=2.6.0"></script>

  </body>
</html>
